/*
 * Copyright (C) 2010-2012 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
 * limitations under the License.
  */
package trs.wmata.api;

import trs.wmata.BusStop;

import java.io.Serializable;
import java.util.ArrayList;

import org.xml.sax.Attributes;

public final class BusStopSchedule extends ApiResponse implements Serializable {
    private static final long serialVersionUID = -3453873206293799737L;
    public static final String DIRECTIONNUM = "DirectionNum";
    public static final String ENDTIME = "EndTime";
    public static final String LAT = "Lat";
    public static final String LON = "Lon";
    public static final String NAME = "Name";
    public static final String ROUTEID = "RouteID";
    public static final String ROUTES = "Routes";
    public static final String SCHEDULEARRIVALS = "ScheduleArrivals";
    public static final String SCHEDULETIME = "ScheduleTime";
    public static final String STARTTIME = "StartTime";
    public static final String STOP = "Stop";
    public static final String STOPID = "StopID";
    public static final String STOPSCHEDULEARRIVAL = "StopScheduleArrival";
    public static final String STOPSCHEDULEINFO = "StopScheduleInfo";
    public static final String TRIPDIRECTIONTEXT = "TripDirectionText";
    public static final String TRIPHEADSIGN = "TripHeadsign";
    public static final String TRIPID = "TripID";
    public static final String A_STRING = "string";
    private final BusStop stop;
    private final ArrayList<String> routeCodes = new ArrayList<String>(10);

    public BusStopSchedule() {
        stop = new BusStop();
    }

    @Override
    public void startElement(final String lName, final Attributes atts) {
        //if (DIRECTIONNUM.equals(lName)) {
        //} else if (ENDTIME.equals(lName)) {
        //} else if (LAT.equals(lName)) {
        //} else if (LON.equals(lName)) {

        /*} else  */ if (NAME.equals(lName)) {
            doCaptureChars = true;

            //} else if (ROUTEID.equals(lName)) {
            //} else if (ROUTES.equals(lName)) {
            //} else if (SCHEDULEARRIVALS.equals(lName)) {
            //} else if (SCHEDULETIME.equals(lName)) {
            //} else if (STARTTIME.equals(lName)) {
            //} else if (STOP.equals(lName)) {
        } else if (STOPID.equals(lName)) {
            doCaptureChars = true;

            //} else if (STOPSCHEDULEARRIVAL.equals(lName)) {
            //} else if (STOPSCHEDULEINFO.equals(lName)) {
            //} else if (TRIPDIRECTIONTEXT.equals(lName)) {
            //} else if (TRIPHEADSIGN.equals(lName)) {
            //} else if (TRIPID.equals(lName)) {
        } else if (A_STRING.equals(lName)) {
            doCaptureChars = true;
        }
    }

    @Override
    public void endElement(final String lName, final StringBuilder buff) {
        //if (DIRECTIONNUM.equals(lName)) {
        //} else if (ENDTIME.equals(lName)) {
        //} else if (LAT.equals(lName)) {
        //} else if (LON.equals(lName)) {

        /*} else */ if (NAME.equals(lName)) {
            final String str = buff.toString().trim();
            stop.setName(str);
            doCaptureChars = false;

            //} else if (ROUTEID.equals(lName)) {
        } else if (ROUTES.equals(lName)) {
            stop.setRouteCodes(routeCodes.toArray(new String[routeCodes.size()]));

            //} else if (SCHEDULEARRIVALS.equals(lName)) {
            //} else if (SCHEDULETIME.equals(lName)) {
            //} else if (STARTTIME.equals(lName)) {
            //} else if (STOP.equals(lName)) {
        } else if (STOPID.equals(lName)) {
            final String str = buff.toString().trim();
            stop.setCode(str);
            doCaptureChars = false;

            //} else if (STOPSCHEDULEARRIVAL.equals(lName)) {
            //} else if (STOPSCHEDULEINFO.equals(lName)) {
            //} else if (TRIPDIRECTIONTEXT.equals(lName)) {
            //} else if (TRIPHEADSIGN.equals(lName)) {
            //} else if (TRIPID.equals(lName)) {
        } else if (A_STRING.equals(lName)) {
            final String str = buff.toString().trim();
            routeCodes.add(str);
            doCaptureChars = false;
        }
    }

    private boolean doCaptureChars = false;

    @Override
    public boolean doCaptureChars() {
        return doCaptureChars;
    }

    public BusStop getBusStop() {
        return stop;
    }
}
